
from XmlData2DictData import Xml2Dict
import numpy as np
import pandas as pd
from nd_utils.vec_similarity import *

from const_data import *

class Feature_coauthor_extration():

    def __init__(self, xml2dict):
        self.xml2dict = xml2dict

        self.publication_dict = self.xml2dict.publication_dict
        self.paperidx_list = self.xml2dict.paperidx_list
        self.paperid_idx_dict = self.xml2dict.paperid_idx_dict
        self.fullName = self.xml2dict.fullName

        self.init_graph = self.xml2dict.init_graph

        self.paper_coauthorlist_dict = self._get_paper_coauthorlist_dict()
        self.coauthorset_list = self._get_coauthorset_list()

        self.paper_coauthorvec_matrix = self._get_paper_coauthorvec_matrix()
        self.paper_coauthor_cooccur_num_matrix = self._get_paper_coauthor_cooccur_num_matrix()


    """
        idx :  coauthor list
    """
    def _get_paper_coauthorlist_dict(self):
        paper_coauthorlist_dict = {}

        for paperid, publication_attr_dict in self.publication_dict.items():
            authors_list = publication_attr_dict['authors'].split(',')

            if self.fullName in authors_list:
                authors_list.remove(self.fullName)

            paper_coauthorlist_dict[self.paperid_idx_dict[paperid]] = authors_list

        return paper_coauthorlist_dict

    """
        all coauthor set 
    """
    def _get_coauthorset_list(self):
        coauthor_set = set()
        for coauthorlist in self.paper_coauthorlist_dict.values():
            for coauthor in coauthorlist:
                coauthor_set.add(coauthor)
        return list(coauthor_set)


    def _get_paper_coauthorvec_matrix(self):
        dim_paper = len(self.paperidx_list)
        dim_coauthor = len(self.coauthorset_list)

        paper_coauthorvec_matrix = pd.DataFrame(data = np.zeros((dim_paper, dim_coauthor)),
                                                index = self.paperidx_list, columns = self.coauthorset_list)

        for idx, coauthorlist in self.paper_coauthorlist_dict.items():
            for coauthor in coauthorlist:
                # pandas is cloumn first, and row second
                paper_coauthorvec_matrix[coauthor][idx] = 1
        return paper_coauthorvec_matrix.values

    def _get_paper_coauthor_cooccur_num_matrix(self):
        dim_paper = len(self.paperidx_list)

        paper_coauthor_cooccur_num_matrix = np.zeros( (dim_paper, dim_paper), dtype=np.int )
        for i in range(self.paper_coauthorvec_matrix.shape[0]-1):
            for j in range(i+1,self.paper_coauthorvec_matrix.shape[0]):
                paper_coauthor_cooccur_num_matrix[i][j] = one_hot_vec_coequalone_num( self.paper_coauthorvec_matrix[i],
                                                                                      self.paper_coauthorvec_matrix[j] )
        # this is bug source  because zero_list
        # only aimed at zero_list
        return paper_coauthor_cooccur_num_matrix + paper_coauthor_cooccur_num_matrix.T



def tezt_Feature_coauthor_extration():
    xml2dict = Xml2Dict(xml_filepath)
    feature_coauthor_extration = Feature_coauthor_extration(xml2dict)

    # 0 column 1 row
    print np.sum(feature_coauthor_extration.paper_coauthor_cooccur_num_matrix, axis=0)

    print np.sum(feature_coauthor_extration.paper_coauthor_cooccur_num_matrix, axis=1)

    print feature_coauthor_extration.paper_coauthor_cooccur_zero_idxlist

    print len(feature_coauthor_extration.paper_coauthor_cooccur_zero_idxlist)


if __name__ == '__main__':
    tezt_Feature_coauthor_extration()